


#ifndef PF_CIRCLE_H
#define PF_CIRCLE_H

#include "pf_atomicentity.h"
#include "lx_Export.h"

/**
 * Holds the data that defines a circle.
 */
class LX_EXPORT PF_CircleAttribute {
public:
    PF_CircleAttribute() {}

    PF_CircleAttribute(const PF_Vector& center,
                  double radius) {

        this->center = center;
        this->radius = radius;
    }

    void reset() {
        center = PF_Vector(false);
        radius = 0.0;
    }

    bool isValid() {
        return (center.valid && radius>PF_TOLERANCE);
    }

    friend class PF_Circle;

    friend std::ostream& operator << (std::ostream& os,
                                      const PF_CircleAttribute& ad) {
        os << "(" << ad.center <<
        "/" << ad.radius <<
        ")";
        return os;
    }

public:
    PF_Vector center;
    double radius;
};



/**
 * Class for a circle entity.
 *

 */
class LX_EXPORT PF_Circle : public PF_AtomicEntity {
public:
    PF_Circle (PF_Container* parent,
               const PF_CircleAttribute& d);
    virtual ~PF_Circle() {}

    virtual PF_Entity* clone() {
        PF_Circle* c = new PF_Circle(*this);
        c->initId();
        return c;
    }

    /**	@return LX_Define::EntityCircle */
    virtual LX_Define::EntityType rtti() const {
        return LX_Define::EntityCircle;
    }
    /** @return true */
    virtual bool isEdge() const {
        return true;
    }

    /** @return Copy of data that defines the circle. **/
    PF_CircleAttribute getData() {
        return data;
    }
    
	virtual PF_VectorSolutions getRefPoints();

	virtual PF_Vector getStartpoint() const {
		return data.center + PF_Vector(data.radius, 0.0);
	}
	virtual PF_Vector getEndpoint() const {
		return data.center + PF_Vector(data.radius, 0.0);
	}
	/** 
	 * @return Direction 1. The angle at which the arc starts at 
	 * the startpoint. 
	 */
	double getDirection1() const {
		return M_PI/2.0;
	}
	/** 
	 * @return Direction 2. The angle at which the arc starts at 
	 * the endpoint.
	 */
	double getDirection2() const {
		return M_PI/2.0*3.0;
	}

    /** @return The center point (x) of this arc */
    PF_Vector getCenter() {
        return data.center;
    }
    /** Sets new center. */
	void setCenter(const PF_Vector& c) {
		data.center = c;
	}
    /** @return The radius of this arc */
    double getRadius() {
        return data.radius;
    }
    /** Sets new radius. */
    void setRadius(double r) {
        data.radius = r;
    }
    double getAngleLength() const;
    virtual double getLength();

    bool createFromCR(const PF_Vector& c, double r);
    bool createFrom2P(const PF_Vector& p1, const PF_Vector& p2);
    bool createFrom3P(const PF_Vector& p1, const PF_Vector& p2,
                      const PF_Vector& p3);

    virtual PF_Vector getNearestEndpoint(const PF_Vector& coord,
                                         double* dist = NULL);
    virtual PF_Vector getNearestPointOnEntity(const PF_Vector& coord,
            bool onEntity = true, double* dist = NULL, PF_Entity** entity=NULL);
    virtual PF_Vector getNearestCenter(const PF_Vector& coord,
                                       double* dist = NULL);
    virtual PF_Vector getNearestMiddle(const PF_Vector& coord,
                                       double* dist = NULL);
    virtual PF_Vector getNearestDist(double distance,
                                     const PF_Vector& coord,
                                     double* dist = NULL);
    virtual PF_Vector getNearestDist(double distance,
                                     bool startp);
    virtual double getDistanceToPoint(const PF_Vector& coord,
                                      PF_Entity** entity=NULL,
                                      LX_Define::ResolveLevel level=LX_Define::ResolveNone,
									  double solidDist = LX_DOUBLEMAX);

    virtual void move(PF_Vector offset);
    virtual void rotate(PF_Vector center, double angle);
    virtual void scale(PF_Vector center, PF_Vector factor);
    virtual void mirror(PF_Vector axisPoint1, PF_Vector axisPoint2);
	virtual void moveRef(const PF_Vector& ref, const PF_Vector& offset);

    virtual void draw(PF_Painter* painter, PF_GraphicView* view, double patternOffset=0.0);

    friend std::ostream& operator << (std::ostream& os, const PF_Circle& a);

    virtual void calculateBorders();

protected:
    PF_CircleAttribute data;
};

#endif
